package com.magic.java8.lambda;


import com.magic.java8.model.DepartmentEnum;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @author laoma
 * @create 2021-03-29 23:03
 */
public class LambdaTest {

    // 语法格式1：无参，无返回值
    @Test
    public void test1() {
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello java 8");
            }
        };
        r1.run();

        System.out.println("===========================");

        Runnable r2 = () -> System.out.println("hello lambda");
        r2.run();

    }

    @Test
    public void test2() {
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1, o2);
            }
        };
        int compare1 = com1.compare(12, 21);
        System.out.println(compare1);

        System.out.println("===========================");
        Comparator<Integer> com2 = (o1, o2) -> Integer.compare(o1, o2);
        int compare2 = com2.compare(32, 21);
        System.out.println(compare2);

        System.out.println("===========================");

        // 方法引用
        Comparator<Integer> com3 = Integer::compare;
        int compare3 = com3.compare(32, 21);
        System.out.println(compare3);
    }

    // 语法格式二：lambda 需要一个参数，但是没有返回值
    @Test
    public void test3() {
        Consumer<String> con = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("hello con");

        System.out.println("===========================");

        Consumer<String> con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("hello con1");
    }

    // 语法格式三：数据类型可以省略，可由编译器推断得出，称为“类型推断”
    @Test
    public void test4() {
        Consumer<String> con2 = s -> {
            System.out.println(s);
        };
        con2.accept("类型推断");
    }

    // 语法格式五：lambda 需要两个或以上的参数，多条执行语句，并且可以有返回值
    @Test
    public void test5() {
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return o1.compareTo(o2);
            }
        };
        System.out.println("com1.compare(12,21) = " + com1.compare(12, 21));

        System.out.println("===========================");

        Comparator<Integer> com2 = ((o1, o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        });

        System.out.println("com2.compare(21,12) = " + com2.compare(21, 12));


    }

    // 语法格式六：当lambda体只有一条语句时候，return 与大括号若有，都可以省略
    @Test
    public void test6() {
        Comparator<Integer> com = ((o1, o2) -> o1.compareTo(o2));
        System.out.println(com.compare(12, 6));
    }

    @Test
    public void test7() {
        happyTime(500, money -> System.out.println("钱:" + money));
    }

    public void happyTime(double money, Consumer<Double> con) {
        con.accept(money);
    }

    @Test
    public void test8() {
        List<String> asList = Arrays.asList("北京", "天津", "东京", "西京");
        List<String> list = filterString(asList, s -> s.contains("京"));
        System.out.println(list);
    }

    // 根据给定的规则，过滤集合中的字符串，此规则由Predicate的方法决定
    public List<String> filterString(List<String> list, Predicate<String> pre) {
        ArrayList<String> filterList = new ArrayList<>();

        list.forEach(s -> {
            if (pre.test(s)) {
                filterList.add(s);
            }
        });

        return filterList;
    }

    @Test
    public void test11() {
        System.out.println("DepartmentEnum.AMZC.getDepartment() = " + DepartmentEnum.AMZC.getDepartment());
        System.out.println("DepartmentEnum.AMZC.getDepartmentId() = " + DepartmentEnum.AMZC.getDepartmentId());
    }
}
